//
//  CommentCell.m
//  DemoMeshtiles
//
//  Created by MAC on 7/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CommentCell.h"

@implementation CommentCell
@synthesize cellHeight;
@synthesize avatar;
@synthesize nameLabel;
@synthesize comment;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //Ve avatar
        avatar=[[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"224.png"]];                
        avatar.frame=CGRectMake(10 , 10, 50, 50);
        [self.contentView addSubview:avatar];
        
        //Ve nick name
        CGRect nameLabelRect = CGRectMake(70, 10, 215-72, 15);
        nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];         
        [nameLabel setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]];
        nameLabel.font = [UIFont boldSystemFontOfSize:11]; 
        nameLabel.textColor=[UIColor blueColor];
        [self.contentView addSubview: nameLabel];
        
        //Ve comment
       // NSString *beforeDecodeStr=[cellData objectForKey:@"content"];       
      //  NSString *commentStr=[beforeDecodeStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        
   //     [commentStr size
         //
        
        
         CGRect commentRect = CGRectMake(70, 30, 310-70, 15);
         comment = [[UILabel alloc] initWithFrame:commentRect]; 
        comment.textColor=[UIColor grayColor];
        comment.font=[UIFont systemFontOfSize:12];
        
  
         [comment setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]]; 
         [self.contentView addSubview:comment];
     
        
    }
    return self;
}
-(void)setCellData:(NSDictionary *)cellData {
    //Update avatar
    NSString *linkURL=[cellData objectForKey:@"url_image"];
    [avatar setImageURL:[NSURL URLWithString:linkURL]];
    nameLabel.text=[cellData objectForKey:@"user_name"];
    //Update comment
    NSString *beforeDecodeStr=[cellData objectForKey:@"content"];       
    NSString *commentStr=[beforeDecodeStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    CGSize maximumLabelSize = CGSizeMake(296,9999);
    CGSize expectedLabelSize = [commentStr sizeWithFont:comment.font 
                                      constrainedToSize:maximumLabelSize 
                                          lineBreakMode:comment.lineBreakMode]; 
    
    //adjust the label the the new height.
    CGRect newFrame = comment.frame;
    newFrame.size.height = expectedLabelSize.height;
    comment.frame = newFrame;
    comment.text=commentStr;
    //
    cellHeight=30+(newFrame.size.height)+5;//bonus 10
    NSLog(@"Set data comment cell, height = %d",cellHeight);
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
